﻿using GameProtocol;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YServer;

namespace ServerApp.Logic.Fight
{
    public class FightHandler:AbsMutiplitySendHandler,IMessageReceive
    {
        /// <summary>
        /// 战斗房间字典
        /// </summary>
        public ConcurrentDictionary<int, FightRoom> fRooms = new ConcurrentDictionary<int, FightRoom>();

        /// <summary>
        /// 连接与战斗房间的映射
        /// </summary>
        public ConcurrentDictionary<YToken, FightRoom> trDic = new ConcurrentDictionary<YToken, FightRoom>();

        /// <summary>
        /// 角色与战斗房间的映射
        /// </summary>
        public ConcurrentDictionary<string, FightRoom> rrDic = new ConcurrentDictionary<string, FightRoom>();


        public ConcurrentStack<FightRoom> fRoomStack = new ConcurrentStack<FightRoom>();

        public ConcurrentInt roomIndex=new ConcurrentInt();


        public FightHandler() {
            FunUtil.createFightDe = CreateRoom;
            FunUtil.removeFightDe = Remove;
        }

        public void OnClientConn(YServer.YToken token)
        {
        }

        public void OnMessageReceive(YServer.YToken token, YServer.SocketModel model)
        {
            if (trDic.ContainsKey(token)) {
                FightRoom room;
                trDic.TryGetValue(token, out room);
                room.OnMessageReceive(token, model);
            }
        }

        public void OnClientClose(YServer.YToken token, string error)
        {
            if (trDic.ContainsKey(token))
            {
                FightRoom room;
                trDic.TryGetValue(token, out room);
                room.OnClientClose(token,error);
            }
        }

        public void CreateRoom(MatchRoom r)
        {
            FightRoom room;
            if (fRoomStack.Count > 0)
            {
                fRoomStack.TryPop(out room);
            }
            else {
                room = new FightRoom();
                roomIndex.GetAndAdd(ref room.roomID);
            }

            fRooms.TryAdd(room.roomID, room);

            //连接与房间的映射
            foreach (YToken item in r.tokens)
            {
                trDic.TryAdd(item, room);
            }

            //角色名与房间的映射
            foreach (string item in r.teamOne)
            {
                rrDic.TryAdd(item, room);
            }
            foreach (string item in r.teamTwo)
            {
                rrDic.TryAdd(item, room);
            }

            room.mat_room = new MatchRoom();
            room.mat_room.id = r.id;
            room.mat_room.role_name = r.role_name;
            room.mat_room.count = r.count;

            string[] t1 = r.teamOne.ToArray<string>();
            string[] t2 = r.teamTwo.ToArray<string>();

            room.mat_room.teamOne.AddRange(t1);
            room.mat_room.teamTwo.AddRange(t2);

            //room.mat_room.tokens.AddRange(ts);
            YToken[] ts = r.tokens.ToArray<YToken>();
            room.tokens.AddRange(ts);
        }

        public void Remove(FightRoom r) {
            if (fRooms.ContainsKey(r.roomID)) {
                r.Clear();
                fRooms.TryRemove(r.roomID,out r);
                fRoomStack.Push(r);
            }

        }


        public override void SetProType()
        {
            type = TypeProtocol.Fight;
        }
    }
}
